package com.cictec.middleware.elasticsearch.camel;

import java.util.Map;

import org.apache.camel.ProducerTemplate;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 这里通过camel的seda创建线程 异步处理信息，也可以直接调用ElasticsearchPositionService里的方法
 * 
 * @author ql
 */
@Component
@Aspect
public class ESPositionServiceAspect {

	public static Logger logger = LoggerFactory.getLogger(ESPositionServiceAspect.class);

	@Autowired
	private MiddlewareESCamelServer middlewareESCamelServer;
	@Autowired
	private ProducerTemplate realCaculateProducerTemplate;
	@Autowired
	private ProducerTemplate busMileageProducerTemplate;
	@Autowired
	private ProducerTemplate producerTemplate;

	/**
	 * 获取原始点位置信息数组，读取第一个和最后一个点的信息
	 * 
	 * @param pjp
	 * @throws Throwable
	 */
	@SuppressWarnings("rawtypes")
	@Around("execution(* com.cictec.middleware.elasticsearch.controller.MiddlewareElasticsearchController.saveRealCaculateEnterOutStation(..))")
	public void saveFirstAndLastPoint(ProceedingJoinPoint pjp) throws Throwable {

		// System.out.println("method start time:" +
		// System.currentTimeMillis());
		// pjp.proceed();// 执行原来逻辑
		// System.out.println("method end time:" + System.currentTimeMillis());

		// logger.debug("aop 重写执行逻辑");
		Map map = (Map) pjp.getArgs()[0];
		// DefaultProducerTemplate
		// SpringCamelContext
		// 这里通过camel的seda创建线程 异步处理信息，也可以直接调用ESPositionService里的方法
		realCaculateProducerTemplate.sendBody(map);

	}

	/**
	 * 更新里程统计信息
	 * 
	 * @param pjp
	 * @throws Throwable
	 */
	@SuppressWarnings("rawtypes")
	@Around("execution(* com.cictec.middleware.elasticsearch.controller.MiddlewareElasticsearchController.saveMileageCaculate(..))")
	public void updateStation(ProceedingJoinPoint pjp) throws Throwable {

		// System.out.println("method start time:" +
		// System.currentTimeMillis());
		// pjp.proceed();// 执行原来逻辑
		// System.out.println("method end time:" + System.currentTimeMillis());

		// logger.debug("aop 重写执行逻辑");
		Map map = (Map) pjp.getArgs()[0];
		/*
		 * {
		 *  "realCaculate" : "busMileage",
		 *  "date" : "日期",
		 *  "busUuid" : "busUuid",
		 *  "busNumber" : "busNumber",
		 *  "saveEnterOut" : "进出站信息json",
		 *  "savePosition" : "位置信息json",
		 * }
		 */
		busMileageProducerTemplate.sendBody(map);
	}
	
	@SuppressWarnings("rawtypes")
	@Around("execution(* com.cictec.middleware.elasticsearch.controller.MiddlewareElasticsearchController.saveTimingPointTM(..))")
	public void saveTimingPointTM(ProceedingJoinPoint pjp) throws Throwable {
		Map map = (Map) pjp.getArgs()[0];
		producerTemplate.sendBody("seda:middleware.gpstm.save.es", map);
	}

}
